/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.table.*;

import Model.*;
import Sort.*;

/**
 *
 * @author pol
 */
public class DriverView extends JFrame {

    public DriverView(Driver[] d, String[] header) {
        super("Driver Info.");
        GridBagLayout layout = new GridBagLayout();
        JPanel panel = new JPanel(layout);

        // Создание компонент
        sortLabel = new JLabel("Select sort method");
        sortCombo = new JComboBox(new String[]{"Insertion sort", "Change sort", "Selection sort"});
        sortButton = new JButton("Sort");

        searchLabel = new JLabel("Enter data for search");
        searchButton = new JButton("Search");
        searchText = new JTextArea();
        searchText.setEditable(true);
        searchText.setBorder(BorderFactory.createEtchedBorder());
        keyCombo = new JComboBox(new String[]{"Name", "Salary", "Experience", "DriverClass"});
        keyLabel = new JLabel("Select sort/search key");

        model = new DriverModel(d, header);
        final JTable table = new JTable(model);


        // Размещение компонент с помощью класса GBC.
        panel.add(sortLabel, new GBC(1, 0).setAnchor(GBC.CENTER).setFill(GBC.CENTER).setWeight(1, 0));
        panel.add(sortCombo, new GBC(1, 1).setAnchor(GBC.WEST).setFill(GBC.VERTICAL));
        panel.add(sortButton, new GBC(2, 1).setFill(GBC.VERTICAL));

        panel.add(searchLabel, new GBC(1, 3, 1, 1).setAnchor(GBC.CENTER).setFill(GBC.CENTER).setWeight(1, 0));
        panel.add(searchText, new GBC(1, 4, 1, 1).setFill(GBC.HORIZONTAL).setAnchor(GBC.NORTHEAST));
        panel.add(searchButton, new GBC(2, 4, 1, 1).setFill(GBC.HORIZONTAL).setAnchor(GBC.NORTHEAST));

        panel.add(searchLabel, new GBC(1, 5, 1, 1).setAnchor(GBC.HORIZONTAL).setAnchor(GBC.NORTHEAST).setFill(GBC.CENTER).setWeight(1, 0));
        panel.add(keyCombo, new GBC(2, 5, 1, 1).setFill(GBC.HORIZONTAL).setAnchor(GBC.NORTHEAST));

        panel.add(new JScrollPane(table), new GBC(0, 0, 1, 6).setFill(GBC.BOTH));

        // Добавление слушателей событий
        sortCombo.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                selectedSortMethod = (String) sortCombo.getSelectedItem();
                System.out.println(selectedSortMethod);
            }
        });

        keyCombo.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                selectedKeyForSortOrSearch = (String) keyCombo.getSelectedItem();
            }
        });

        sortButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (selectedSortMethod.equals("Insertion sort")) {
                    System.out.println("Insertion Sort");
                    if (selectedKeyForSortOrSearch.equals("Name")) {
                        InsertionSort.sort(model.getDriverList(), new CompareByName());
                    } else if (selectedKeyForSortOrSearch.equals("Salary")) {
                        InsertionSort.sort(model.getDriverList(), new CompareBySalary());
                    } else if (selectedKeyForSortOrSearch.equals("Experience")) {
                        InsertionSort.sort(model.getDriverList(), new CompareByExperience());
                    } else if (selectedKeyForSortOrSearch.equals("DriverClass")) {
                        InsertionSort.sort(model.getDriverList(), new CompareByDriverClass());
                    }
                } else if (selectedSortMethod.equals("Change sort")) {
                    System.out.println("Change Sort");
                    if (selectedKeyForSortOrSearch.equals("Name")) {
                        BubbleSort.sort(model.getDriverList(), new CompareByName());
                    } else if (selectedKeyForSortOrSearch.equals("Salary")) {
                        BubbleSort.sort(model.getDriverList(), new CompareBySalary());
                    } else if (selectedKeyForSortOrSearch.equals("Experience")) {
                        BubbleSort.sort(model.getDriverList(), new CompareByExperience());
                    } else if (selectedKeyForSortOrSearch.equals("DriverClass")) {
                        BubbleSort.sort(model.getDriverList(), new CompareByDriverClass());
                    }
                } else if (selectedSortMethod.equals("Selection sort")) {
                    System.out.println("Selection Sort");
                    if (selectedKeyForSortOrSearch.equals("Name")) {
                        SelectionSort.sort(model.getDriverList(), new CompareByName());
                    } else if (selectedKeyForSortOrSearch.equals("Salary")) {
                        SelectionSort.sort(model.getDriverList(), new CompareBySalary());
                    } else if (selectedKeyForSortOrSearch.equals("Experience")) {
                        SelectionSort.sort(model.getDriverList(), new CompareByExperience());
                    } else if(selectedKeyForSortOrSearch.equals("DriverClass")) {
                        SelectionSort.sort(model.getDriverList(), new CompareByDriverClass());
                    }
                }
                table.updateUI();
            }
        });

        add(panel);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        pack();
    }

    private void addButton(String label, ActionListener listener) {
        JButton button = new JButton(label);
        button.addActionListener(listener);
        panel.add(button);
    }
    // Основные компонеты интерфейса
    private JPanel panel;
    private JLabel sortLabel;
    private JLabel searchLabel;
    private JLabel keyLabel;
    private JComboBox sortCombo;
    private JComboBox keyCombo;
    private JButton sortButton;
    private JButton searchButton;
    private JTextArea searchText;
    // Строики в которых хранится текущее значение keyCombo и sortCombo
    private String selectedSortMethod = "Insertion sort";
    private String selectedKeyForSortOrSearch = "Name";
    // Модель данных
    private DriverModel model;
}
